<!-- 设计器实现-申请页 -->
<template>
  <div class="processForm" v-loading="loading">
    <bm-form-realize
      v-if="realizeCreat"
      ref="realize"
      v-model="formData"
      :config="formConfig"
      :headers="token"
      :otherInfo="otherInfo"
      @submit="formSubmit"
      @save="formSave"
    ></bm-form-realize>
  </div>
</template>

<script>
import request from "@/plugins/axios";
import Cookies from "js-cookie";

export default {
  name: "apply",
  data() {
    return {
      // prop
      formData: { historyNode: [] },
      formConfig: {},
      otherInfo: {},
      //insideData
      loading: true,
      query: {}, //传值参数
      totalData: {},
      realizeCreat: false
    };
  },
  computed: {
    token() {
      return {
        "Blade-Auth": "bearer " + Cookies.get("saber-access-token")
      };
    }
  },
  created() {
    //传参
    this.query = this.$route.query;
    if (this.query.appId) {
      this.handlerGetConfig();
    } else {
      this.$message.warning("请先传递参数");
      this.loading = false;
    }
  },
  methods: {
    handlerGetConfig() {
      //获取表单配置
      request({
        method: "get",
        url: "/api/rem-registrationmgrweb/otc-flow/workflowform/detail",
        params: {
          mgrOrBiz: "mgr",
          appId: this.query.appId,
          nodeId: this.query.nodeId
        }
      })
        .then(res => {
          if (res.data.success) {
            this.formConfig = JSON.parse(res.data.data.formData);
            this.formConfig.saveBtn = true;
            this.otherInfo = {
              query: this.query,
              process: res.data.data
            };
            this.realizeCreat = true;
            this.loading = false;

            //草稿 获取表单数据
            if (this.query.statusId) {
              this.loading = true;
              request
                .get("otc-mgr/applyform/getDraftById?id=" + this.query.statusId)
                .then(res => {
                  this.loading = false;
                  if (res.data.success) {
                    let tag = JSON.parse(res.data.data.bizData);
                    this.formData = tag;
                  } else {
                    this.$message.error(res.data.msg || "获取草稿数据失败！");
                  }
                })
                .catch(() => {
                  this.loading = false;
                  this.$message.error("获取草稿数据失败！");
                });
            }
          } else {
            this.loading = false;
            this.$message.error(res.data.msg || "获取表单配置失败！");
          }
        })
        .catch(() => {
          this.loading = false;
          this.$message.error("获取表单配置失败！");
        });
    },
    //表单提交
    formSubmit(data) {
      this.$confirm("您确定要提交吗？", "流程提交", {
        type: "warning"
      }).then(() => {
        this.loading = true;

        let submitData = {};

        //草稿提交
        if (this.query.statusId) {
          submitData = {
            bizData: data,
            extProperty: {
              bizAppId: parseInt(this.query.statusId)
            }
          };
        } else {
          submitData = {
            bizData: data
          };
        }

        request
          .post(
            `api/otc-mgr/applyform/submitInner/${this.query.appId}`,
            submitData
          )
          .then(res => {
            this.loading = false;
            if (res.data.success) {
              this.$message.info("提交成功");
              this.$router.push("/queryPage/bjotcProcessMonitor");
            } else {
              this.$message.error(res.data.msg || "提交失败");
            }
          })
          .catch(() => {
            this.loading = false;
            this.$message.error("提交失败!");
          });
      });
    },
    //保存草稿
    formSave(data) {
      this.$confirm("您确定要保存吗？", "流程保存", {
        type: "warning"
      }).then(() => {
        this.loading = true;
        let submitData = {
          bizData: JSON.stringify(data),
          bizType: this.query.appId,
          applyTitle: this.otherInfo.process.formName + "申请"
        };
        request
          .post(`api/otc-mgr/applyform/saveDraft`, submitData)
          .then(res => {
            this.loading = false;
            if (res.data.success) {
              this.$message.info("保存成功");
              //跳转我的申请
              //this.$router.push("/queryPage/bjotcProcessMonitor");
            } else {
              this.$message.error(res.data.msg || "提交失败");
            }
          })
          .catch(() => {
            this.loading = false;
            this.$message.error("提交失败!");
          });
      });
    }
  }
};
</script>


<style lang="scss">
.processForm {
  min-height: 70vh;
  margin: 10px 0;
  background: white;
  padding: 20px;
}
</style>
